<template>
  <t-dialog
    v-model:visible="show"
    :width="900"
    :header="plugin.t('create')"
    :onConfirm="() => form.submit()"
    destroyOnClose
    :closeOnOverlayClick="false"
  >
    <template #body>
      <div class="plugin-reports-form">
        <t-alert class="alert" theme="info">
          <span v-for="item in plugin.tm('alert.create')">{{ item }}</span>
        </t-alert>
        <t-form label-align="top">
          <t-form-item :label="plugin.t('form.time')">
            <t-select v-model="time">
              <t-option v-for="item in timeList" :value="item" :label="item.toString()" />
            </t-select>
          </t-form-item>
          <t-form-item :label="plugin.t('form.type')">
            <t-row :gutter="15">
              <t-col :span="3" v-for="(item, index) in plugin.tm('form.type_text')" :key="index">
                <div class="row-box flash" :class="{ on: type == index }" @click="type = index">
                  <t-popup placement="top-left" :content="plugin.t('form.type_message.' + index)" showArrow>
                    <div class="name wrap">
                      {{ item }}
                    </div>
                    <div class="item wrap">
                      {{ plugin.t('form.type_message.' + index) }}
                    </div>
                  </t-popup>
                </div>
              </t-col>
            </t-row>
          </t-form-item>
        </t-form>
      </div>
    </template>
    <template #footer>
      <t-button theme="primary" v-if="!loading" @click="onSubmit">
        {{ $t('common.create') }}
      </t-button>
      <t-progress theme="plump" :percentage="progress" status="active" v-else />
    </template>
  </t-dialog>
</template>
<script setup>
  import { ref } from 'vue'
  import app from '@/app'
  import index from '../index'
  import reportsStore from '../store'
  import createFactory from '../common'

  const plugin = index.plugin

  const show = ref(false)
  const loading = ref(false)
  const progress = ref(0)
  const type = ref('order')
  const timeList = [
    app.time.now().year(),
    app.time.now().subYear(1).year(),
  ]
  const time = ref(timeList[0])
  

  const open = () => {
    show.value = true
  }

  const onSubmit = async() => {
    loading.value = true
    const factory = createFactory(type.value, time.value)
    try{
      await factory.pull((total, index) => {
        progress.value = parseInt(index / total * 100)
      })
      app.message('success', 'message.create')
    } catch(e) {
      console.error(e)
      app.message('warning', plugin.message('message.error'))
    }

    await reportsStore.reset(1)
    loading.value = false
  }

  defineExpose({ open })
</script>
<style lang="scss">
  @import '@/style/global';
  .plugin-reports-form{
    overflow: hidden;

    .alert{
      margin-bottom: 20px;
      span{
        display: block;
        line-height: 25px;
      }
    }

    .row-box{
      cursor: pointer;
      border: 1px solid $color-border;
      padding: 5px 12px;
      margin-bottom: 15px;
      border-radius: 5px;
      background-color: var(--td-bg-color-container);

      &:hover, &.on{
        border-color: $color;
      }
    }

    .name{
      color: $color-title;
      font-size: 12px;
      font-weight: bold;
      line-height: 20px;
    }
    .item{  
      color: $color-content;
      font-size: 12px;
    }
  }
</style>